package net.kazed.nextaction.view;

import net.kazed.nextaction.view.TextViewListBuilder.TextViewBuilder;
import android.content.Context;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class TextViewListBuilder {

   private LayoutInflater inflater;
   private int separatorLayout;
   private int itemLayout;
   private ViewGroup viewGroup;
   private boolean first = true;

   public TextViewListBuilder(final Context context) {
      inflater = (LayoutInflater) context.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
   }
   
   public TextViewListBuilder separatorLayout(final int separatorLayout) {
      this.separatorLayout = separatorLayout;
      return this;
   }

   public TextViewListBuilder textViewResource(final int itemLayout) {
      this.itemLayout = itemLayout;
      return this;
   }

   public TextViewListBuilder listContainer(final ViewGroup viewGroup) {
      this.viewGroup = viewGroup;
      return this;
   }

   public TextViewBuilder createTextView() {
      if (first) {
         first = false;
      } else {
         if (separatorLayout != 0) {
            View separatorView = inflater.inflate(separatorLayout, viewGroup, false);
            viewGroup.addView(separatorView);
         }
      }
      View view = inflater.inflate(itemLayout, viewGroup, false);
      viewGroup.addView(view);

      return new TextViewBuilder(view);
   }
   
   public class TextViewBuilder {
      private View view;
      
      public TextViewBuilder(final View view) {
         this.view = view;
      }
      
      public TextViewBuilder background(final int resource) {
         view.setBackgroundResource(resource);
         return this;
      }
      
      public TextViewBuilder text(final int textViewResource, final String text) {
         TextView textView = (TextView) view.findViewById(textViewResource);
         textView.setText(text);
         return this;
      }
      
      public TextViewBuilder text(final int textViewResource, final Spanned text) {
         TextView textView = (TextView) view.findViewById(textViewResource);
         textView.setText(text);
         return this;
      }
      
      public TextViewBuilder text(final int textViewResource, final int stringResource) {
         TextView textView = (TextView) view.findViewById(textViewResource);
         textView.setText(stringResource);
         return this;
      }
      
      public ImageViewBuilder image(final int imageResource, final int drawableResource) {
         ImageView imageView = (ImageView) view.findViewById(imageResource);
         imageView.setImageResource(drawableResource);
         return new ImageViewBuilder(imageView);
      }
      
      public ImageViewBuilder image(final int imageResource) {
          ImageView imageView = (ImageView) view.findViewById(imageResource);
          return new ImageViewBuilder(imageView);
       }
       
      public TextViewBuilder onClickListener(final View.OnClickListener listener) {
         view.setOnClickListener(listener);
         return this;
      }

      public View getView() {
         return view;
      }
   }
   
   public class ImageViewBuilder {
	   private ImageView imageView;
	   
	   public ImageViewBuilder(final ImageView imageView) {
		this.imageView = imageView;
	   }

	   public ImageViewBuilder invisible() {
		   imageView.setVisibility(View.INVISIBLE);
		   return this;
	   }
	   
	   public ImageViewBuilder onClickListener(final View.OnClickListener listener) {
		   imageView.setOnClickListener(listener);
		   return this;
	   }

	   public ImageViewBuilder background(final int resource) {
		   imageView.setBackgroundResource(resource);
		   return this;
	   }
   }
}
